package org.small.shop.phone.api.process.users.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.small.shop.phone.api.process.coupon.mapper.TblWxCouponMapper;
import org.small.shop.phone.api.process.coupon.pojo.TblWxCoupon;
import org.small.shop.phone.api.process.openuser.mapper.TblWxOpenUserMapper;
import org.small.shop.phone.api.process.openuser.pojo.TblWxOpenUser;
import org.small.shop.phone.api.process.orders.mapper.TblWxOrdersMapper;
import org.small.shop.phone.api.process.orders.pojo.SelectOrders;
import org.small.shop.phone.api.process.userrank.mapper.TblWxUserrankMapper;
import org.small.shop.phone.api.process.userrank.pojo.TblWxUserrank;
import org.small.shop.phone.api.process.users.mapper.TblWxUsersMapper;
import org.small.shop.phone.api.process.users.pojo.HomeUsersVo;
import org.small.shop.phone.api.process.users.pojo.TblWxUsers;
import org.small.shop.phone.api.utils.idworker.Sid;
import org.small.shop.phone.api.utils.objectcope.ObjectCopeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TblWxUsersServiceImpl implements TblWxUsersService {

	@Autowired
	private TblWxUsersMapper usersMapper;

	@Autowired
	private TblWxOpenUserMapper tblWxOpenUserMapper;

	@Autowired
	private TblWxOrdersMapper tblwxOrdersMapper;

	@Autowired
	private TblWxUserrankMapper tblWxUserrankMapper;

	@Autowired
	private TblWxCouponMapper tblWxCouponMapper;

	@Override
	public List<TblWxUsers> getAllUsers() {

		return usersMapper.selectAll();
	}

	@Override
	public TblWxUsers queryIsExist(String openid) {

		return usersMapper.queryIsExist(openid);
	}

	@Override
	@Transactional
	public void addingUsers(String openid) {

		String userId = Sid.nextShort();
		// 组装注册信息
		TblWxUsers tblWxUsers = new TblWxUsers();
		tblWxUsers.setUserId(userId);
		tblWxUsers.setUserLastloginTime(System.currentTimeMillis());
		tblWxUsers.setUserCreateTime(System.currentTimeMillis());
		tblWxUsers.setUserTatolscore(0);
		tblWxUsers.setUserStatus(1);
		tblWxUsers.setUserTotalamt(new BigDecimal(0));
		usersMapper.insertSelective(tblWxUsers);

		TblWxOpenUser tblWxOpenUser = new TblWxOpenUser();
		tblWxOpenUser.setOpenid(openid);
		tblWxOpenUser.setUserid(userId);
		tblWxOpenUser.setIsLoginStatus(1);
		tblWxOpenUserMapper.insertSelective(tblWxOpenUser);

	}

	@Override
	@Transactional
	public void updateUsers(String openid, TblWxUsers users) throws Exception {

		TblWxOpenUser openUser = tblWxOpenUserMapper.selectByPrimaryKey(openid);
		if (openUser != null && !StringUtils.isEmpty(openUser.getUserid())) {
			TblWxUsers toUsers = new TblWxUsers();
			toUsers.setUserId(openUser.getUserid());
			ObjectCopeUtils.copyProperties(users, toUsers);
			usersMapper.updateByPrimaryKeySelective(toUsers);
		} else {
			throw new Exception();
		}
	}

	@Override
	public HomeUsersVo getHomeUser(String openid) throws Exception {

		if (!StringUtils.isEmpty(openid)) {

			TblWxUsers tblWxUsers = usersMapper.getUserNameAndPhoneByAppId(openid);
			if (tblWxUsers != null) {
				HomeUsersVo homeUsersVo = new HomeUsersVo();
				homeUsersVo.setUserNickname(tblWxUsers.getUserNickname());
				homeUsersVo.setImginfoPath(tblWxUsers.getImginfoPath());
				// 获取
				SelectOrders selectOrders = new SelectOrders();
				selectOrders.setUserId(tblWxUsers.getUserId());
				selectOrders.setOrdStauts(-2);
				Integer unpaidNum = tblwxOrdersMapper.selectAllOrdsByUserIdOrStaCount(selectOrders);
				selectOrders.setOrdStauts(0);
				Integer undeliveredNum = tblwxOrdersMapper.selectAllOrdsByUserIdOrStaCount(selectOrders);
				selectOrders.setOrdStauts(1);
				Integer unreceivedNumber = tblwxOrdersMapper.selectAllOrdsByUserIdOrStaCount(selectOrders);

				homeUsersVo.setUnpaidNum(unpaidNum);
				homeUsersVo.setUndeliveredNum(undeliveredNum);
				homeUsersVo.setUnreceivedNumber(unreceivedNumber);

				// 会员信息
				TblWxUserrank tblWxUserrank = tblWxUserrankMapper.selectByPrimaryKey(tblWxUsers.getRankId());
				if (tblWxUserrank != null) {
					homeUsersVo.setRankName(tblWxUserrank.getRankName());
				}

				// 优惠券信息
				List<TblWxCoupon> couponList = tblWxCouponMapper.getAllCouponList(tblWxUsers.getUserId());
				homeUsersVo.setConponList(couponList);

				return homeUsersVo;
			}
		}
		throw new Exception();
	}

	@Override
	public HomeUsersVo getUserInfo(String openid) throws Exception {

		if (!StringUtils.isEmpty(openid)) {

			TblWxUsers tblWxUsers = usersMapper.getUserNameAndPhoneByAppId(openid);

			if (tblWxUsers != null) {
				HomeUsersVo homeUsersVo = new HomeUsersVo();
				homeUsersVo.setUserNickname(tblWxUsers.getUserNickname());
				homeUsersVo.setUserPhone(tblWxUsers.getUserPhone());
				homeUsersVo.setUserTatolscore(tblWxUsers.getUserTatolscore());
				homeUsersVo.setUserTotalamt(tblWxUsers.getUserTotalamt());
				homeUsersVo.setImginfoPath(tblWxUsers.getImginfoPath());

				// 会员信息
				TblWxUserrank tblWxUserrank = tblWxUserrankMapper.selectByPrimaryKey(tblWxUsers.getRankId());
				if (tblWxUserrank != null) {
					homeUsersVo.setRankName(tblWxUserrank.getRankName());
				}
				return homeUsersVo;
			}
		}
		throw new Exception();
	}

	@Override
	@Transactional
	public void updataUserNickname(String openid, String userNickname) throws Exception {

		if (!StringUtils.isEmpty(openid)) {

			TblWxUsers tblWxUsers = usersMapper.getUserNameAndPhoneByAppId(openid);
			if (tblWxUsers != null && tblWxUsers.getUserStatus() != null && tblWxUsers.getUserStatus() == 1) {

				Map<String, String> map = new HashMap<String, String>();
				map.put("userNickname", userNickname);
				map.put("userId", tblWxUsers.getUserId());
				usersMapper.updataUserNickname(map);
				return;
			}
		}
		throw new Exception();
	}

	@Override
	public void uploadUserHeadImg(String openid, String uploadPathDB) throws Exception {

		if (!StringUtils.isEmpty(openid)) {
			TblWxUsers tblWxUsers = usersMapper.getUserNameAndPhoneByAppId(openid);
			if (tblWxUsers != null && tblWxUsers.getUserStatus() != null && tblWxUsers.getUserStatus() == 1) {

				Map<String, String> map = new HashMap<String, String>();
				map.put("imginfoPath", uploadPathDB);
				map.put("userId", tblWxUsers.getUserId());
				usersMapper.uploadUserHeadImg(map);
				return;
			}
		}
		throw new Exception();
	}
}
